﻿using Microsoft.AspNetCore.Mvc;
using ScanCode;
using System.Reflection;
using System.Runtime.InteropServices;

namespace Imgeye.mogul.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class CodeController : ControllerBase
    {


        [DllImport("D:\\BarCode\\libzbar-0.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern IntPtr zbar_image_scanner_create();

        [HttpPost, HttpGet]
        [Route("ScanBarCode")]
        public IActionResult ScanBarCode()
        {
            string str = "";
            try
            {
                //// 获取当前执行程序的路径
                //string currentDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

                //// 构建 libzbar-0.dll 的相对路径
                //string libzbarPath = Path.Combine(currentDirectory, "libzbar-0.dll");

                // 使用相对路径加载 DLL
                //IntPtr scanner = zbar_image_scanner_create();

                str += "第一步";
            
                zbar_image_scanner_create();

                str += "第二步";
                var code = BarCode.ScanBarCode("C:\\Img\\2.jpg");
                return Ok(code);
            }
            catch (Exception ex)
            {
                return Ok(str + ex.Message);
            }
        }
    }
}
